#include <filesystem>
#include <iostream>
#include <string>
#include <map>
#include<fstream>
namespace fs = std::filesystem;

void editText();
void bookcatalog();
void bookshelf();


std::string encryptPassword(const std::string& password) {
	return password;
}
class Work {
	friend void editText();
public:
	Work() : name(), likes(0) {};

	void display() const {
		std::cout << "章节名称: " << name << ", 点赞数: " << likes << std::endl;
	}

	void askForLike() {
		std::string input;
		std::cout << "你喜欢这个章节吗？(yes/no): ";
		std::cin >> input;

		if (input == "yes" || input == "Yes") {
			likes++;
			std::cout << "感谢你的点赞！" << std::endl;
		}
		else if (input == "no" || input == "No") {
			std::cout << "感谢你的反馈，我们会继续努力！" << std::endl;
		}
		else {
			std::cout << "无效的输入，请重新输入！" << std::endl;
			askForLike(); // 如果输入无效，重新询问
		}
	}

private:
	std::string name;
	int likes;
};

namespace fs = std::filesystem;

void listTxtFiles(const fs::path& path) {
	if (fs::exists(path) && fs::is_directory(path)) {
		for (const auto& entry : fs::directory_iterator(path)) {
			if (entry.is_regular_file() && entry.path().extension() == ".txt") {
				std::cout << entry.path().filename();
				std::cout << "(" << entry.path() << ")" << std::endl;
			}
		}
	}
	else {
		std::cerr << "指定的路径不存在或不是一个目录。" << std::endl;
		bookcatalog();
	}
}

void  bookcatalog() {
	std::string inputPath;
	std::cout << "想打开的书（请输入书的的路径）: ";
	std::cin >> inputPath;

	try {
		listTxtFiles(inputPath);
	}
	catch (const fs::filesystem_error& e) {
		std::cerr << "文件系统错误: " << e.what() << std::endl;
	}
	catch (const std::exception& e) {
		std::cerr << "发生异常: " << e.what() << std::endl;
	}
	editText();
}


void listFolders(const fs::path& path) {
	if (fs::exists(path) && fs::is_directory(path)) {
		for (const auto& entry : fs::directory_iterator(path)) {
			if (fs::is_directory(entry.status())) {
				// 获取文件夹名称
				std::string folderName = entry.path().filename().string();
				// 获取文件夹的完整路径
				std::string folderPath = entry.path().string();
				// 输出文件夹名称和路径
				std::cout << folderName << " (" << folderPath << ")" << std::endl;
			}
		}
	}
	else {
		std::cerr << "指定的路径不存在或不是一个目录。" << std::endl;
		bookshelf();
	}
}
void bookshelf() {
	std::string pathToSearch;
	std::cout << "请输入要搜索的目录路径: ";
	std::cin >> pathToSearch;

	// 将输入的字符串转换为fs::path对象
	fs::path searchPath(pathToSearch);

	// 调用函数来列出目录下的所有文件夹
	listFolders(searchPath);
	
}

void editText() {
	std::string filename,name;
	std::cout << "请输入要打开或保存的章节（不包括路径，不需要扩展名.txt）: ";
	std::cin >> filename;
	name = filename;
	filename += ".txt";
	std::ifstream file(filename);
	if (file.is_open()) {
		std::string line;
		while (getline(file, line)) { // 逐行读取
			std::cout << line << std::endl; // 显示内容
		}
		file.close(); // 关闭文件
	}
	else {
		std::cerr << "无法打开文件" << std::endl;
		editText();
	}
	Work myWork;
	myWork.name = name;
	myWork.display(); // 显示初始状态
	myWork.askForLike(); // 询问用户是否点赞
	myWork.display(); // 显示更新后的状态
}

class UserLoginSystem {
private:
	std::map<std::string, std::string> users; // 存储用户名和密码的映射

public:
	// 注册新用户
	void registerUser() {
		std::string username, password;
		std::cout << "请输入用户名: ";
		std::cin >> username;

		if (users.find(username) != users.end()) {
			std::cout << "用户名已存在，请重新输入。\n";
			return;
		}

		std::cout << "请输入密码: ";
		std::cin >> password;

		// 加密密码并存储
		users[username] = encryptPassword(password);
		std::cout << "注册成功！\n";
	}

	// 用户登录
	bool loginUser() {
		std::string username, password;
		std::cout << "请输入用户名: ";
		std::cin >> username;

		std::cout << "请输入密码: ";
		std::cin >> password;

		// 检查用户名和密码是否匹配
		if (users.find(username) != users.end() && users[username] == encryptPassword(password)) {
			std::cout << "登录成功！\n";
			return true;
		}
		else {
			std::cout << "用户名或密码错误，登录失败。\n";
			return false;
		}
	}

	// 显示菜单并处理用户输入
	void run() {
		int choice;
		do {
			std::cout << "\n--- 用户登录系统 ---\n";
			std::cout << "1. 注册新用户\n";
			std::cout << "2. 用户登录\n";
			std::cout << "3. 退出\n";
			std::cout << "请选择操作: ";
			std::cin >> choice;

			switch (choice) {
			case 1:
				registerUser();
				break;
			case 2:
				if (loginUser()) {
					bookshelf();
					bookcatalog();
				}
				break;
			case 3:
				std::cout << "退出系统...\n";
				break;
			default:
				std::cout << "无效的选择，请重新输入。\n";
			}
		} while (choice != 3);
	}
};

int main() {
	UserLoginSystem loginSystem;
	loginSystem.run();
	return 0;
}